﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 入院 : Form
    {
        SqlConnection conn = new SqlConnection(DBHelper.connString);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public 入院()
        {
            InitializeComponent();
        }
        public void Fill()
        {
            string sql = "select Patient.HosNo,PatName,PatSex,BronYear,PatId,PatAdr from Patient";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void 入院_Load(object sender, EventArgs e)
        {
            Fill();
        }

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_bqzt.Text = "";
            tb_ch.Text = "";
            tb_fb.Text = "";
            tb_ks.Text = "";
            tb_nl.Text = "";
            dtp_ryrq.Text = "";
            dtp_ssrq.Text = "";
            tb_szzb.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_yjje.Text = "";
            tb_ys.Text = "";
            tb_zd.Text = "";
            tb_zrhs.Text = "";
            tb_ZYNo.Text = "";
            cb_hldj.Text = "";
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            int year = dt.Year;
            string HosNo = tb_ZYNo.Text.Trim();
            string PatName = tb_xm.Text.Trim();
            string PatSex = tb_xb.Text.Trim();
            string PatAge = tb_nl.Text.Trim();
            int Age = Convert.ToInt32(PatAge);
            int ds = year - Age;
            string BronYear = ds.ToString();
            string BedNo = tb_ch.Text.Trim();
            DateTime HosIndate = dtp_ryrq.Value;
            string HosArea = tb_szzb.Text.Trim();
            string HosPrepay = tb_yjje.Text.Trim();
            string PatType = tb_fb.Text.Trim();
            DateTime HosSurgerydate = dtp_ssrq.Value;
            string HosRank = cb_hldj.Text.Trim();
            string HosCondition = tb_bqzt.Text.Trim();
            string HosIllness = tb_zd.Text.Trim();
            string DName = tb_ys.Text.Trim();
            string NName = tb_zrhs.Text.Trim();
            string a = tb_ch.Text.Trim();
            int b = a.Length;
            string c = a.Substring(0, (b - 1));
            string BedRoom = c;
            string BedSubject = tb_ks.Text.Trim();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string DNo = "select DocNo from Doctor where DocName ='" + DName + "' ";
                string NNo = "select NurNo from Nurse where NurName ='" + NName + "' ";
                string BNo = "select BedNo from Bed where BedNo ='" + BedNo + "' ";
                SqlCommand D = new SqlCommand(DNo, conn);
                SqlCommand N = new SqlCommand(NNo, conn);
                SqlCommand B = new SqlCommand(BNo, conn);
                conn.Open();
                string DD = Convert.ToString(D.ExecuteScalar());
                string NN = Convert.ToString(N.ExecuteScalar());
                string BB = Convert.ToString(B.ExecuteScalar());
                string sql = string.Format("insert into Patient values ('{0}','{1}','{2}','{3}','{4}','{5}')", HosNo, PatName, PatSex, BronYear, null, null);
                string str = string.Format("insert into InHospital values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')", HosNo, BedNo, HosIndate, HosArea, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness, null, null, DD, NN, null);

                string abc = string.Format("update Bed set BedCondition='已用'where Bed.BedNo='" + BB + "'");
                SqlCommand bed = new SqlCommand(abc, conn);

                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand com = new SqlCommand(str, conn);


                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int z = Convert.ToInt32(bed.ExecuteNonQuery());
                if (n == 1 && m == 1 && z == 1)
                {
                    MessageBox.Show("保存成功！");
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }

            }

        }




    }
}
